//
//  RPSettingViewController.m
//  RPCFCADemo_Example
//
//  Created by 宋鸿康 on 2022/10/19.
//  Copyright © 2022 john. All rights reserved.
//

#import "RPSettingViewController.h"
#import "RPSettingView.h"

@interface RPSettingViewController ()
@property(nonatomic, strong) UIButton *loginBtn;
@property(nonatomic, strong) RPSettingView *companyView; /// 运营机构
@property(nonatomic, strong) RPSettingView *organView; /// 企业代码
@property(nonatomic, strong) RPSettingView *loginAccountView; /// 登入账号
@property(nonatomic, strong) RPSettingView *serverUrlView; /// 服务器地址
@property(nonatomic, strong) RPSettingView *serverPortView; /// 端口
@end

@implementation RPSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.fd_prefersNavigationBarHidden = YES;
    
    LLNavgationBarView *nav = [LLNavgationBarView addBarTo:self.view];
    nav.shadowView.backgroundColor = [UIColor colorWithHexString:@"#DEDEDE"];
    nav.title = @"企业配置";
    
    MyLinearLayout *layout = [self createVertSubviewLayout];
    layout.topPos.equalTo(@92);
    layout.widthSize.equalTo(self.view.widthSize);
    [self.view addSubview:layout];
}


- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setBackgroundColor:[UIColor colorWithHexString:@"4BA8FF"]];
        [_loginBtn setTitle:@"保存" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        _loginBtn.layer.cornerRadius = 5.0f;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn setTitleColor:[UIColor colorWithHexString:@"#EEEEEE"] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (void)loginBtn:(UIButton *)btn{
    NSString *compnay = [self.companyView getResult];
    NSString *organ = [self.organView getResult];
    NSString *acount = [self.loginAccountView getResult];
    NSString *serverUrl = [self.serverUrlView getResult];
    NSString *port = [self.serverPortView getResult];

    if (!compnay.isNotBlank) {
        [QMUITips showError:@"运营机构不能为空" inView:self.view hideAfterDelay:2];
    }else if (!organ.isNotBlank){
        [QMUITips showError:@"企业代码不能为空" inView:self.view hideAfterDelay:2];
    }else if (!acount.isNotBlank){
        [QMUITips showError:@"登入账号不能为空" inView:self.view hideAfterDelay:2];
    }else if (!serverUrl.isNotBlank){
        [QMUITips showError:@"服务器地址不能为空" inView:self.view hideAfterDelay:2];
    }else if (!port.isNotBlank){
        [QMUITips showError:@"服务器端口不能为空" inView:self.view hideAfterDelay:2];
    }else{
        RPDataManager.sharedInstance.company = compnay;
        RPDataManager.sharedInstance.organ  = organ;
        RPDataManager.sharedInstance.acount  = acount;
        RPDataManager.sharedInstance.port = port;
        RPDataManager.sharedInstance.url = serverUrl;
        [QMUITips showInfo:@"设置成功" inView:self.view];
        !_popBlock ? : _popBlock();
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
}
/**
 * 创建一个垂直的线性子布局。
 */
-(MyLinearLayout*)createVertSubviewLayout
{
    //创建垂直布局视图。
    MyLinearLayout *vertLayout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
//    vertLayout.backgroundColor = [CFTool color:0];

    RPSettingView *v1 = [[RPSettingView alloc] init];
    [v1 setText:RPDataManager.sharedInstance.company];
    self.companyView = v1;
    v1.myWidth = MyLayoutSize.fill;
    v1.myHeight = 70;     //设置布局尺寸
    [v1 updateByRow:0];
    [vertLayout addSubview:v1];
    
    RPSettingView *v2 = [[RPSettingView alloc] init];
    [v2 setText:RPDataManager.sharedInstance.organ];
    self.organView = v2;
    v2.myWidth = MyLayoutSize.fill;
    v2.myHeight = 70;     //设置布局尺寸
    [v2 updateByRow:1];
    [vertLayout addSubview:v2];
    
    RPSettingView *v3 = [[RPSettingView alloc] init];
    [v3 setText:RPDataManager.sharedInstance.acount];
    self.loginAccountView = v3;
    v3.myWidth = MyLayoutSize.fill;
    v3.myHeight = 70;     //设置布局尺寸
    [v3 updateByRow:2];
    [vertLayout addSubview:v3];
    
    RPSettingView *v4 = [[RPSettingView alloc] init];
    [v4 setText:RPDataManager.sharedInstance.url];
    self.serverUrlView = v4;
    v4.myWidth = MyLayoutSize.fill;
    v4.myHeight = 70;     //设置布局尺寸
    [v4 updateByRow:3];
    [vertLayout addSubview:v4];
    
    RPSettingView *v5 = [[RPSettingView alloc] init];
    [v5 setText:RPDataManager.sharedInstance.port];
    self.serverPortView = v5;
    v5.myWidth = MyLayoutSize.fill;
    v5.myHeight = 70;     //设置布局尺寸
    [v5 updateByRow:4];
    [vertLayout addSubview:v5];
    
    UIButton *v6 = self.loginBtn;
    v6.myTop = 35.0f;
    v6.myLeft = 20;
    v6.myRight = 20;
    v6.myHeight = 40;     //设置布局尺寸
    [vertLayout addSubview:v6];
    
    return vertLayout;
}

@end
